import request from '@/utils/request';
import type { BasicMsg, BasicMsgWithData, Favorite } from '@/types';

// 获取用户的所有收藏
export const getUserFavorites = (userId: number): Promise<BasicMsgWithData<Favorite[]>> => {
  return request({
    url: `/api/favorites/user/${userId}`,
    method: 'get'
  });
};

// 添加收藏
export const addFavorite = (data: Favorite): Promise<BasicMsg> => {
  return request({
    url: '/api/favorites',
    method: 'post',
    data
  });
};

// 取消收藏
export const deleteFavorite = (id: number): Promise<BasicMsg> => {
  return request({
    url: `/api/favorites/${id}`,
    method: 'delete'
  });
};

// 根据用户ID和菜品ID取消收藏
export const deleteFavoriteByUserAndDish = (userId: number, dishId: number): Promise<BasicMsg> => {
  return request({
    url: `/api/favorites/user/${userId}/dish/${dishId}`,
    method: 'delete'
  });
};

// 检查用户是否收藏了某个菜品
export const checkFavorite = (userId: number, dishId: number): Promise<BasicMsgWithData<boolean>> => {
  return request({
    url: `/api/favorites/user/${userId}/dish/${dishId}`,
    method: 'get'
  });
};

// 获取菜品的收藏数量
export const getDishFavoriteCount = (dishId: number): Promise<BasicMsgWithData<number>> => {
  return request({
    url: `/api/favorites/dish/${dishId}/count`,
    method: 'get'
  });
};

